<?php
class Cscandir{
    public $path;
	public $completeFiles ;
	public $completeDirs ;
	public $files;
	public $dirs ;
	public $filesDirs ;
	public $completeFilesDirs ;
	
	//把文件转化为文件所在目录字串
	//eg: I:/AppServ/www/_App_/inc/baseLib.php
    //变为 I:/AppServ/www/_App_/inc/
	public static function fileToDirname($path){
	    if( is_file($path) ){
		    return dirname($path);	
		} 
		return $path;
	}
    public function  __construct($path="I:/_phplib/",$sortingOrder=0){
		$path = Cscandir::fileToDirname($path);
		$this->path = $path ;
		$this->scandir($path , $sortingOrder);
		$this->getCompleteFilesDirs();
		$this->getCompleteDirs();
		return $this->filesDirs;
	}
	//  第一步
	public function  scandir($path='',$sortingOrder=0){
		if(empty($path)){
		    $path=$this->path;	
		}
		//获取目录下的所有文件s和文件夹s  scandir可用dir替代
 		$this->filesDirs=scandir ( $path , $sortingOrder ) ;  
		return $this->filesDirs;       
		//sort()升序  rsort()降序 ;
	}
	
	//得到完整路径的目录和文件
	public function getCompleteFilesDirs(){
		if(count($this->completeFilesDirs)==0){
			foreach($this->filesDirs as $k => $v ){
				$this->completeFilesDirs[] = self::formatPath($this->path."/".$v);   	
			}
		}
		return $this->completeFilesDirs;
		
	} 
	public function getFiles($regexp=false){
		$fils=array();
	        foreach($this->filesDirs as $k => $v ){
				if(!is_dir($this->path."/".$v)  ){
					 
					$fils[] =$v ; 

				}
			}	
		return $fils;
	}

	public function getDirs(){
		$dirs=array();
	        foreach($this->filesDirs as $k => $v ){
				if(is_dir($this->path."/".$v))
				$dirs[] =$v ;   	
			}	
		return $dirs;
	}
	
	//得到完整的目录文件名
	public function getCompleteDirs(){

		if(count($this->completeDirs)==0){
			foreach($this->completeFilesDirs as $k=>$v){			
				if(is_dir($v)){
					$this->completeDirs[]=$v;
				}else{
					$this->completeFiles[]=$v;	
				}	
			}
		}
		return $this->completeDirs;
		
	}
	public function getCompleteFiles(){
		return $this->completeFiles;
	}	
	public static function formatPath($str){		
	    return preg_replace (     '/\/{1,}/' , '/' ,
	           str_replace ( '\\' , '/' , $str )   ) ;	
    }	 
}
?>